package com.ext.aic.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.recyclerview.widget.RecyclerView;

import com.ext.aic.AICManager;
import com.ext.aic.R;
import com.ext.aic.beans.WhiteListItem;
import com.ext.aic.beans.WhiteListItem_;
import com.ext.aic.databinding.WhiteListItemViewBinding;
import com.ext.aic.holder.WhiteListViewHolder;
import com.ext.aic.ui.AddWhiteListActivity;
import com.ext.aic.utils.RedirectUtils;
import com.ext.aic.utils.XPopupUtils;
import com.eyinfo.android_pure_utils.utils.ConvertUtils;

import java.util.ArrayList;
import java.util.List;

public class WhiteListAdapter extends RecyclerView.Adapter<WhiteListViewHolder> {

    private Context context;

    public WhiteListAdapter(Context context) {
        this.context = context;
    }

    private List<WhiteListItem> items = new ArrayList<>();

    public void clear() {
        items.clear();
    }

    public void addItems(List<WhiteListItem> items) {
        this.items.addAll(items);
    }

    @Override
    public WhiteListViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.white_list_item_view, parent, false);
        return new WhiteListViewHolder(WhiteListItemViewBinding.bind(view));
    }

    @Override
    public void onBindViewHolder(WhiteListViewHolder holder, int position) {
        holder.itemLl.setOnClickListener(this::onItemClick);
        holder.itemLl.setTag(position);
        holder.removeTv.setOnClickListener(this::onRemoveWhiteListClick);
        holder.removeTv.setTag(position);

        WhiteListItem whiteListItem = items.get(position);
        holder.wechatUserTv.setText(whiteListItem.getUser());
    }

    public void onItemClick(View view) {
        int position = ConvertUtils.toInt(view.getTag());
        WhiteListItem whiteListItem = items.get(position);

        Bundle bundle = new Bundle();
        bundle.putSerializable("whiteList", whiteListItem);
        RedirectUtils.getInstance().startActivity(view.getContext(), AddWhiteListActivity.class, bundle);
    }

    @SuppressLint("NotifyDataSetChanged")
    public void onRemoveWhiteListClick(View view) {
        int position = ConvertUtils.toInt(view.getTag());
        WhiteListItem whiteListItem = items.get(position);

        String title = context.getString(R.string.tip);
        String content = context.getString(R.string.remove_white_list_tip_format, whiteListItem.getUser());
        XPopupUtils.getInstance().showConfirmDialog(context, title, content, false, () -> {
            WhiteListItem remove = items.remove(position);
            notifyDataSetChanged();

            AICManager.getInstance().delete(WhiteListItem.class, queryBuilder ->
                    queryBuilder.equal(WhiteListItem_.id, remove.getId())
            );
        });
    }

    @Override
    public int getItemCount() {
        return items.size();
    }
}
